<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('imrStudentInfoVerify.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.accountId')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.studentCode')" prop="studentCode">
        <el-input
          v-model="formData.studentCode"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.studentCode')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.studentName')" prop="studentName">
        <el-input
          v-model="formData.studentName"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.studentName')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.studentGrade')" prop="studentGrade">
        <el-input
          v-model="formData.studentGrade"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.studentGrade')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.studentClass')" prop="studentClass">
        <el-input
          v-model="formData.studentClass"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.studentClass')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.verifyType')" prop="verifyType">
        <el-select
          v-model="formData.verifyType"
          :placeholder="
            t('common.selectTextPlaceholder') + t('imrStudentInfoVerify.fields.verifyType')
          "
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.verifyStatus')" prop="verifyStatus">
        <el-radio-group v-model="formData.verifyStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.modifyFields')" prop="modifyFields">
        <el-input
          v-model="formData.modifyFields"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.modifyFields')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.oldValue')" prop="oldValue">
        <el-input
          v-model="formData.oldValue"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.oldValue')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.newValue')" prop="newValue">
        <el-input
          v-model="formData.newValue"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.newValue')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.remark')" prop="remark">
        <el-input
          v-model="formData.remark"
          :placeholder="t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.remark')"
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.reviewer')" prop="reviewer">
        <el-input
          v-model="formData.reviewer"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.reviewer')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.reviewerName')" prop="reviewerName">
        <el-input
          v-model="formData.reviewerName"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.reviewerName')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.reviewTime')" prop="reviewTime">
        <el-date-picker
          v-model="formData.reviewTime"
          type="date"
          value-format="x"
          :placeholder="
            t('common.selectTextPlaceholder') + t('imrStudentInfoVerify.fields.reviewTime')
          "
        />
      </el-form-item>
      <el-form-item :label="t('imrStudentInfoVerify.fields.reviewComments')" prop="reviewComments">
        <el-input
          v-model="formData.reviewComments"
          :placeholder="
            t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.reviewComments')
          "
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ImrStudentInfoVerifyApi, ImrStudentInfoVerifyVO } from '@/api/core/imrstudentinfoverify'
import { FormRules } from 'element-plus'

/** 联培学生信息变更审批 表单 */
defineOptions({ name: 'ImrStudentInfoVerifyForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  studentCode: undefined,
  studentName: undefined,
  studentGrade: undefined,
  studentClass: undefined,
  verifyType: undefined,
  verifyStatus: undefined,
  modifyFields: undefined,
  oldValue: undefined,
  newValue: undefined,
  remark: undefined,
  reviewer: undefined,
  reviewerName: undefined,
  reviewTime: undefined,
  reviewComments: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.id'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.accountId'),
      trigger: 'blur'
    }
  ],
  studentCode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.studentCode'),
      trigger: 'blur'
    }
  ],
  studentName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.studentName'),
      trigger: 'blur'
    }
  ],
  studentGrade: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.studentGrade'),
      trigger: 'blur'
    }
  ],
  studentClass: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.studentClass'),
      trigger: 'blur'
    }
  ],
  verifyType: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.verifyType'),
      trigger: 'change'
    }
  ],
  verifyStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.verifyStatus'),
      trigger: 'blur'
    }
  ],
  modifyFields: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.modifyFields'),
      trigger: 'blur'
    }
  ],
  oldValue: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.oldValue'),
      trigger: 'blur'
    }
  ],
  newValue: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.newValue'),
      trigger: 'blur'
    }
  ],
  remark: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.remark'),
      trigger: 'blur'
    }
  ],
  reviewer: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.reviewer'),
      trigger: 'blur'
    }
  ],
  reviewerName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.reviewerName'),
      trigger: 'blur'
    }
  ],
  reviewTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.reviewTime'),
      trigger: 'blur'
    }
  ],
  reviewComments: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.reviewComments'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('imrStudentInfoVerify.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ImrStudentInfoVerifyApi.getImrStudentInfoVerify(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ImrStudentInfoVerifyVO
    if (formType.value === 'create') {
      await ImrStudentInfoVerifyApi.createImrStudentInfoVerify(data)
      message.success(t('common.createSuccess'))
    } else {
      await ImrStudentInfoVerifyApi.updateImrStudentInfoVerify(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    studentCode: undefined,
    studentName: undefined,
    studentGrade: undefined,
    studentClass: undefined,
    verifyType: undefined,
    verifyStatus: undefined,
    modifyFields: undefined,
    oldValue: undefined,
    newValue: undefined,
    remark: undefined,
    reviewer: undefined,
    reviewerName: undefined,
    reviewTime: undefined,
    reviewComments: undefined
  }
  formRef.value?.resetFields()
}
</script>
